package com.imooc.component;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * 消费类
 *
 * @author: 顾志杰
 */
@Component
public class RabbitReceive {

    /**
     * 组合使用监听
     * @param message
     * @param channel
     * @throws Exception
     */
    @RabbitHandler
    @RabbitListener(
            bindings = @QueueBinding(
                    /*队列*/
                    value = @Queue(value = "queue-1", durable = "true"),
                    /*交换机*/
                    exchange = @Exchange(
                            name = "exchange-1",
                            declare = "true",
                            type = "topic",
                            ignoreDeclarationExceptions = "true"),
                    key = "springboot.*")
    )
    public void onMessage(Message message, Channel channel) throws Exception {
        /*1.收到消息以后进行业务端消费处理*/
        System.out.println("----------------------------------------");
        System.out.println("消费信息:"+message.getPayload());
        /*2.处理成功后获取deliverTag 并进行收工的ACK操作 因为配置文件配置的手动签收模式*/
        Long deliverTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliverTag,false);
    }
}
